package 线性结构.util;

public class ArrayList<E> implements IList<E> {

    private E[] elements;

    private int size;

    private final static int DEFAULT_CAPACITY = 10;
    private final static int DEFAULT_NOT_FOUND = -1;


    public ArrayList(int capacity) {
        if (capacity < DEFAULT_CAPACITY) {
            capacity = DEFAULT_CAPACITY;
        }
        elements = (E[]) new Object[capacity];
    }

    public ArrayList() {
        this(DEFAULT_CAPACITY);
    }

    @Override
    public void add(E element) {
        add(size, element);
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);

        E old = elements[index];
        //如果移除的是中间元素，需要把后面的元素往前移
        for (int i = index; i < size; i++) {
            elements[i] = elements[i + 1];
        }
        size--;
        //不需要把最后一个对象置为null，循环已经做了
//        elements[size] = null;
        return old;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckForAdd(index);
        ensureCapacity(size + 1);
        //如果插入的地方是中间，需要把后面的元素往后移
        for (int i = size; i < index; i--) {
            elements[i + 1] = elements[i];
        }

        elements[index] = element;
        size++;
    }

    @Override
    public void set(int index, E element) {
        rangeCheck(index);
        elements[index] = element;
    }

    @Override
    public E get(int index) {
        rangeCheck(index);

        return elements[index];
    }

    @Override
    public void clear() {
        for (int i = 0; i < size; i++) {
            elements[i] = null;
        }
        size = 0;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public int indexOf(E element) {
        if (element == null) {
            for (int i = 0; i < elements.length; i++) {
                if (elements[i] == null) return i;
            }
        } else {
            for (int i = 0; i < elements.length; i++) {
                // ==比较的是地址值，
                // equal 默认也是比较地址值，但如果对象自己实现equal那就根据对象来
                if (element.equals(elements[i])) return i;
            }
        }

        return DEFAULT_NOT_FOUND;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean contains(E element) {
        return indexOf(element) != DEFAULT_NOT_FOUND;
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("size:").append(size).append(" [");
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                stringBuilder.append(",");
            }
            stringBuilder.append(elements[i]);
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }

    private void ensureCapacity(int index) {
        int oldCapacity = elements.length;
        if (index > oldCapacity) {
            //新容量 = 旧容量乘以1.5
            int newCapacity = oldCapacity + (oldCapacity >> 1);

            E[] newElements = (E[]) new Object[newCapacity];
            for (int i = 0; i < size; i++) {
                newElements[i] = elements[i];
            }

            elements = newElements;
            System.out.println(oldCapacity + "扩容为" + newCapacity);
        }
    }

    private void rangeCheck(int index) {
        if (index < 0 || index >= size) {
            throw new ArrayIndexOutOfBoundsException("size:" + size + "index:" + index);
        }
    }

    private void rangeCheckForAdd(int index) {
        if (index < 0 || index > size) {
            throw new ArrayIndexOutOfBoundsException("size:" + size + "index:" + index);
        }
    }
}
